/*
 *
 * Copyright (C) 2012 Walter Lütgenau
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * 
 *      http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 * 
 */
package offset.nodes.client.virtual.model.jcr;

import java.io.InputStream;
import java.util.Calendar;
import javax.jcr.AccessDeniedException;
import javax.jcr.InvalidItemStateException;
import javax.jcr.Item;
import javax.jcr.ItemExistsException;
import javax.jcr.ItemNotFoundException;
import javax.jcr.ItemVisitor;
import javax.jcr.Node;
import javax.jcr.Property;
import javax.jcr.ReferentialIntegrityException;
import javax.jcr.RepositoryException;
import javax.jcr.Session;
import javax.jcr.Value;
import javax.jcr.ValueFormatException;
import javax.jcr.lock.LockException;
import javax.jcr.nodetype.ConstraintViolationException;
import javax.jcr.nodetype.NoSuchNodeTypeException;
import javax.jcr.nodetype.NodeType;
import javax.jcr.nodetype.PropertyDefinition;
import javax.jcr.version.VersionException;
import offset.nodes.client.model.StringArrayConverter;

/**
 *
 * @author Walter Lütgenau
 */
public class SimpleProperty implements Property {
    String name;
    /**
     * The single value string may encode multiple values via StringArrayConverter. 
     * An alternative would be to encode 
     */
    String value;
    
    public SimpleProperty( String name, String value ) {
        this.name = name;
        this.value = value;
    }

    public SimpleProperty( String name, boolean value ) throws RepositoryException {
        this.name = name;
        setValue(value);
    }

    public SimpleProperty( String name, String[] value ) {
        this.name = name;
        this.value = new StringArrayConverter(value).toString();
    }

    public boolean getBoolean() throws ValueFormatException, RepositoryException {
        return value.equalsIgnoreCase(Boolean.TRUE.toString());
    }

    public Calendar getDate() throws ValueFormatException, RepositoryException {
        throw new UnsupportedOperationException("Not supported yet.");
    }
    
    class SimplePropertyDefinition implements PropertyDefinition {

        public NodeType getDeclaringNodeType() {
            throw new UnsupportedOperationException("Not supported yet.");
        }

        public String getName() {
            throw new UnsupportedOperationException("Not supported yet.");
        }

        public int getOnParentVersion() {
            throw new UnsupportedOperationException("Not supported yet.");
        }

        public boolean isAutoCreated() {
            throw new UnsupportedOperationException("Not supported yet.");
        }

        public boolean isMandatory() {
            throw new UnsupportedOperationException("Not supported yet.");
        }

        public boolean isProtected() {
            throw new UnsupportedOperationException("Not supported yet.");
        }

        public Value[] getDefaultValues() {
            throw new UnsupportedOperationException("Not supported yet.");
        }

        public int getRequiredType() {
            throw new UnsupportedOperationException("Not supported yet.");
        }

        public String[] getValueConstraints() {
            throw new UnsupportedOperationException("Not supported yet.");
        }

        public boolean isMultiple() {
            if (new StringArrayConverter(value).isArray(value) )
                return true;
            return false;
        }
        
    }
    

    public PropertyDefinition getDefinition() throws RepositoryException {
        return new SimplePropertyDefinition();
    }

    public double getDouble() throws ValueFormatException, RepositoryException {
        throw new UnsupportedOperationException("Not supported yet.");
    }

    public long getLength() throws ValueFormatException, RepositoryException {
        throw new UnsupportedOperationException("Not supported yet.");
    }

    public long[] getLengths() throws ValueFormatException, RepositoryException {
        throw new UnsupportedOperationException("Not supported yet.");
    }

    public long getLong() throws ValueFormatException, RepositoryException {
        throw new UnsupportedOperationException("Not supported yet.");
    }

    public Node getNode() throws ValueFormatException, RepositoryException {
        throw new UnsupportedOperationException("Not supported yet.");
    }

    public InputStream getStream() throws ValueFormatException, RepositoryException {
        throw new UnsupportedOperationException("Not supported yet.");
    }

    public String getString() {
        return (String) value;
    }

    public int getType() throws RepositoryException {
        throw new UnsupportedOperationException("Not supported yet.");
    }

    public Value getValue() throws ValueFormatException, RepositoryException {
        throw new UnsupportedOperationException("Not supported yet.");
    }

    public Value[] getValues() {
        StringArrayConverter sa = new StringArrayConverter((String) value);
        return sa.toValueArray();
    }

    public void setValue(Value arg0) throws ValueFormatException, VersionException, LockException, ConstraintViolationException, RepositoryException {
        throw new UnsupportedOperationException("Not supported yet.");
    }

    public void setValue(Value[] arg0) throws ValueFormatException, VersionException, LockException, ConstraintViolationException, RepositoryException {
        throw new UnsupportedOperationException("Not supported yet.");
    }

    public void setValue(String value) {
        this.value = value;
    }

    public void setValue(String[] values) {
        StringArrayConverter sa = new StringArrayConverter(values);
        value = sa.toString();
   }

    public void setValue(InputStream arg0) throws ValueFormatException, VersionException, LockException, ConstraintViolationException, RepositoryException {
        throw new UnsupportedOperationException("Not supported yet.");
    }

    public void setValue(long arg0) throws ValueFormatException, VersionException, LockException, ConstraintViolationException, RepositoryException {
        throw new UnsupportedOperationException("Not supported yet.");
    }

    public void setValue(double arg0) throws ValueFormatException, VersionException, LockException, ConstraintViolationException, RepositoryException {
        throw new UnsupportedOperationException("Not supported yet.");
    }

    public void setValue(Calendar arg0) throws ValueFormatException, VersionException, LockException, ConstraintViolationException, RepositoryException {
        throw new UnsupportedOperationException("Not supported yet.");
    }

    public void setValue(boolean arg0) throws ValueFormatException, VersionException, LockException, ConstraintViolationException, RepositoryException {
        if (arg0)
            setValue(Boolean.TRUE.toString());
        else
            setValue(Boolean.FALSE.toString());
    }

    public void setValue(Node arg0) throws ValueFormatException, VersionException, LockException, ConstraintViolationException, RepositoryException {
        throw new UnsupportedOperationException("Not supported yet.");
    }

    public void accept(ItemVisitor arg0) throws RepositoryException {
        throw new UnsupportedOperationException("Not supported yet.");
    }

    public Item getAncestor(int arg0) throws ItemNotFoundException, AccessDeniedException, RepositoryException {
        throw new UnsupportedOperationException("Not supported yet.");
    }

    public int getDepth() throws RepositoryException {
        throw new UnsupportedOperationException("Not supported yet.");
    }

    public String getName() {
        return name;
    }

    public Node getParent() throws ItemNotFoundException, AccessDeniedException, RepositoryException {
        throw new UnsupportedOperationException("Not supported yet.");
    }

    public String getPath() throws RepositoryException {
        throw new UnsupportedOperationException("Not supported yet.");
    }

    public Session getSession() throws RepositoryException {
        throw new UnsupportedOperationException("Not supported yet.");
    }

    public boolean isModified() {
        throw new UnsupportedOperationException("Not supported yet.");
    }

    public boolean isNew() {
        throw new UnsupportedOperationException("Not supported yet.");
    }

    public boolean isNode() {
        throw new UnsupportedOperationException("Not supported yet.");
    }

    public boolean isSame(Item arg0) throws RepositoryException {
        throw new UnsupportedOperationException("Not supported yet.");
    }

    public void refresh(boolean arg0) throws InvalidItemStateException, RepositoryException {
        throw new UnsupportedOperationException("Not supported yet.");
    }

    public void remove() throws VersionException, LockException, ConstraintViolationException, RepositoryException {
        throw new UnsupportedOperationException("Not supported yet.");
    }

    public void save() throws AccessDeniedException, ItemExistsException, ConstraintViolationException, InvalidItemStateException, ReferentialIntegrityException, VersionException, LockException, NoSuchNodeTypeException, RepositoryException {
        throw new UnsupportedOperationException("Not supported yet.");
    }

}
